# -*- coding: utf-8 -*-
"""
工具栏组件
"""
from PySide6.QtWidgets import QWidget, QFrame, QHBoxLayout

from ..base import TgComponentMixin
from ..buttons import TgButton
from ..containers import TgFrame
from ..layouts import TgHBoxLayout
from ...style_manager import StyleManager


class TgToolBar(QWidget, TgComponentMixin):
    """TG工具栏组件"""
    
    def __init__(self, parent=None):
        super().__init__(parent)
        # 初始化TG组件属性
        self._component_name = self.__class__.__name__
        self._is_initialized = False
        self._style_applied = False
        self._theme = 'default'
        self._size_hint = 'medium'
        self._component_parts = {}
        
        # 初始化组件
        self.initialize()
    
    def setup_ui(self):
        """初始化UI"""
        layout = TgHBoxLayout(margins='small', spacing='small')
        self.setLayout(layout)
        
        # 设置固定高度
        self.setFixedHeight(50)
    
    def apply_style(self):
        """应用样式"""
        # 设置工具栏样式
        self.setStyleSheet(f"""
            QWidget {{
                background-color: {StyleManager.COLORS['light']};
                border-bottom: 1px solid {StyleManager.COLORS['border']};
                padding: {StyleManager.SPACING['sm']};
            }}
        """)
        self._style_applied = True
    
    def add_component_part(self, name, component):
        """添加组件部分"""
        self._component_parts[name] = component
    
    def get_component_part(self, name):
        """获取组件部分"""
        return self._component_parts.get(name)
    
    def get_component_parts(self):
        """获取所有组件部分"""
        return self._component_parts.copy()
    
    def add_button(self, text, theme='primary', size='small'):
        """添加按钮到工具栏"""
        button = TgButton(text, theme, size)
        self.add_component_part(f'button_{text}', button)
        layout = self.layout()
        if layout and hasattr(layout, 'addWidget'):
            layout.addWidget(button)
        return button
    
    def add_separator(self):
        """添加分隔符"""
        separator = TgFrame('line')
        separator.setFrameStyle(QFrame.Shape.VLine)
        separator.setFixedWidth(1)
        self.add_component_part(f'separator_{len(self.get_component_parts())}', separator)
        layout = self.layout()
        if layout and hasattr(layout, 'addWidget'):
            layout.addWidget(separator)
    
    def add_spacer(self):
        """添加弹性空间"""
        layout = self.layout()
        if isinstance(layout, QHBoxLayout):
            layout.addStretch() 